//服务器、数据库模拟




//sessionStorage 的增删改
var sStorage = (sKey, sValue) => {
    let noSave = sessionStorage.getItem(sKey);
    if (sValue == '-delete') {
        essionStorage.removeItem(sKey);
    }
    if (!noSave) {
        sessionStorage.setItem(sKey, sValue);
    } else {

        sessionStorage.setItem(sKey, sValue);

    }

}


////查loacl库
var getLocalStarged = (dothing, id) => { //查内容 查指定组信息
    switch (dothing) {
        case '查内容库'://参数boxid；返回，暂存中boxid的所有数据
            return {
                // 通过boxid拿到localstarged
                className: localStorage.getItem(id + '_ content_className'),
                contentBody: localStorage.getItem(id + '_content_body'),
                user: localStorage.getItem(id + '_content_User'),
                time: localStorage.getItem(id + '_content_time'),
                reditTime: localStorage.getItem(id + '_ReditTime'),
                reditDate: localStorage.getItem(id + '_ReditDete'),
                root: localStorage.getItem(id + '_Root'),
                head:localStorage.getItem(id + '_content_head'),
            }
        case '查指定组信息': //返回组ID，组名和User的组权限
            let user = sessionStorage.getItem('userName');
            let root = classMeteStorage(id, '查询', user);//需要改 不能用
            return { //参数id；返回
                classid: id,
                className: localStorage.getItem(id + '_className'),
                classRoot: root,
            }


    }


}


var classMsgStorage = (classid, className, master) => { //组信息表增删改
    //组ID，组名，创组人
    let classmete = localStorage.getItem(classid + "_mete");
    if (classmete == null) { //初始化
        classmete = master;
        let boxidStr = '';
        localStorage.setItem(classid + '_className', className);
        localStorage.setItem(classid + '_master', master);
        localStorage.setItem(classid + '_boxidStr', boxidStr);
    }


}

var classMeteStorage = (classid, operation, user) => { //组员信息表的增删改
    //operation：新建，组长，管理，组员，职位查询
    //let meteAll = classid+'mete':zkqzwxec22,'组长'，liushangji,'管理'；
    var doingUser = sessionStorage.getItem('userName');
    let admin = 3;
    let mete = 50;
    var inClass = false;
    var inClassPosition;
    let meteName = [];
    let meteRoot = [];
    let meteStr = localStorage.getItem(classid + '_mete');
    var newMeteAll = [];

    if (meteStr == null) { //新建初始化
        if (operation == '新建') {
            localStorage.setItem(classid + '_mete', doingUser + ',' + '组长');
            return
        }
        return console.log('错误：组中没有人员');
    }

    let meteAll =createStr(meteStr,',');
    for (var i = 0; i < meteAll.length; i++) {
        if ((i + 1) % 2 != 0) {
            meteName.push(meteAll[i]);
        } else {
            meteRoot.push(meteAll[i]);
        }

    }

    switch (operation) {

        case '组长': //更换组长的操作只能由原组长完成,且新组长必须为组员、管理
            for (var i = 0; i < meteName.length; i++) {
                if (meteName[i] == user) {
                    inClass = true;
                    inClassPosition = i;
                }
            }
            if (!inClass) {
                return console.log('该组员不在小组中');
            }
            for (var i = 0; i < meteRoot.length; i++) { //判断操作者是否为组长
                if (meteRoot[i] == '组长') {
                    if (meteName[i] == doingUser) {
                        meteRoot[i] = '组员';

                    } else {
                        return '操作者不是组长'
                    };
                }
            }
            meteName.splice(inClassPosition, 1);
            meteName.push(user);
            meteRoot.splice(inClassPosition, 1);
            meteRoot.push('组长');
            break

        case '管理':
            var isAdminNum = 0;
            for (var i = 0; i < meteName.length; i++) {
                if (meteName[i] == user) {
                    inClass = true;
                    inClassPosition = i;
                }
            }
            if (!inClass) {
                return console.log('该组员不在小组中');
            }

            for (var i = 0; i < meteRoot.length; i++) {
                if (meteRoot[i] == '管理') {
                    isAdminNum++;
                }
            }
            if (isAdminNum >= admin) {
                return console.log('管理职位已达三人上限');
            }
            for (var i = 0; i < meteRoot.length; i++) { //判断操作者是否为组长
                if (meteRoot[i] == '组长' || meteRoot[i] == '管理') {
                    if (meteName[i] == doingUser) {

                        meteRoot[inClassPosition] = '管理';

                    } else {
                        return '操作者不是组长或管理'
                    };
                }
            }
            break

        case '组员':
            var inClass = false;
            var inClassPosition;
            for (var i = 0; i < meteName.length; i++) {
                if (meteName[i] == user) {
                    inClass = true;
                    inClassPosition = i;
                }
            }
            if (!inClass) {

                meteName.push(user);
                meteRoot.push('组员');
                return;
            } else {
                meteRoot[inClassPosition] == '组员'

            }
            break

        case '查询': //查询成员在组中的职位
            for (var i = 0; i < meteName.length; i++) {
                if (meteName[i] == user) {
                    inClass = true;
                    inClassPosition = i;
                }
            }
            if (inClass == false) {
                return console.log('该组员不在小组中');
            }
            for (var i = 0; i < meteName.length; i++) {
                if (meteName[i] == user) {

                    return meteRoot[i]
                }
            }
    }

    for (var i = 0; i < meteName.length; i++) { //更新组员表
        let update = meteName[i] + ',' + meteRoot[i] + ',';
        newMeteAll.push(update);
    }
    localStorage.setItem(classid + '_mete', newMeteAll);

}

var MsglocalStorage = {


}

//日志内容表的操作
function contentMsgStorage(classid, classUser, doing, contentBody, contentHead){//内容栏的增删改
    //doing = 'new' 时 需要的参数依次排列 classid classUser 'new' '' ''
    //doing = 'add' 参数依次排列  classid classUser 'add' contentBody contentHead
    //doing = edit 时修改  
    //doing = delete 删除 boxid '' 'delete' '' '';
    let content_boxid = Date.now() + classid + classUser;
    let content_User = classUser;
    let content_body = contentBody;
    let content_head = contentHead;
    let content_time = DateN();
    let content_ReditTime = '0';
    let content_ReditDete = DateN();

    
    function neW(){ //公告板
        let noSave = localStorage.getItem(classid + '_boxidStr');
        if(noSave != ''){
            return console.log('错误');;
        }
        boxidStr = content_boxid;
        content_head = '公告板'
        content_body = '公告板内容'
        localStorage.setItem(classid + '_boxidStr', boxidStr);
        localStorage.setItem(classid+ '_father', classid);
        localStorage.setItem(content_boxid + '_content_body', content_body);
        localStorage.setItem(content_boxid + '_content_head', content_head);
        localStorage.setItem(content_boxid + '_content_User', classUser);
        localStorage.setItem(content_boxid + '_content_time', content_time);
        localStorage.setItem(content_boxid + '_ReditTime', content_ReditTime);
        localStorage.setItem(content_boxid + '_ReditDete', content_time);
        return;
    };

    function add(){ //新增内容栏
        let boxidStr = localStorage.getItem(classid + '_boxidStr'); 
        if (boxidStr == '') {
            boxidStr = content_boxid;
        } else {
            boxidStr += ',' + content_boxid;
        }

        localStorage.setItem(classid + '_boxidStr', boxidStr);
        localStorage.setItem(content_boxid + '_father', classid);
        localStorage.setItem(content_boxid + '_content_body', contentBody);
        localStorage.setItem(content_boxid + '_content_head', content_head);
        localStorage.setItem(content_boxid + '_content_User', classUser);
        localStorage.setItem(content_boxid + '_content_time', content_time);
        localStorage.setItem(content_boxid + '_ReditTime', content_ReditTime);
        localStorage.setItem(content_boxid + '_ReditDete', content_ReditDete);
        return;
    };

    function edit(){ //修改
        let boxid = classid;
        let times = parseInt(localStorage.getItem(boxid + '_ReditTime')) + 1;
        localStorage.setItem(boxid + '_content_body', contentBody);
        localStorage.setItem(boxid + '_content_head', content_head);
        localStorage.setItem(boxid + '_ReditTime', times);
        localStorage.setItem(boxid + '_ReditDete', DateN());
    };

    function deletE(){
        let boxid = classid;
        updateBoxId('delete',boxid);
        localStorage.removeItem(boxid + '_content_body');
        localStorage.removeItem(boxid + '_father');
        localStorage.removeItem(boxid + '_content_head');
        localStorage.removeItem(boxid + '_content_User');
        localStorage.removeItem(boxid + '_content_time');
        localStorage.removeItem(boxid + '_ReditTime');
        localStorage.removeItem(boxid + '_ReditDete');

    

    }

    switch (doing) {
        case 'new':
            neW();
            break;
        case 'add':
            add();
            break;
        case 'delete':
        deletE();
            break;
        default:
            edit();
            break;
    }


}

//更新classid的boxid串 
function updateBoxId(doing,boxid){
          switch(doing){
            case 'add':
              var id = localStorage(boxid+'_father');
              var boxidStr = localStorage.getItem(id+ '_boxidStr'); 
              boxidStr += boxid+',';
              localStorage.setItem(classid + '_boxidStr',boxidStr);
              break;
              case 'delete':
                var id = localStorage.getItem(boxid+'_father');
              var boxidStr = localStorage.getItem(id+ '_boxidStr'); 
              let boxidArr = createStr(boxidStr,',');
              var newboxIdStr;
              for(var i = 0;i<boxidArr.length;i++){
                    if(boxidArr[i] != boxid){
                      newboxIdStr = boxidArr[i]+','
                    }
              }
              localStorage.setItem(id + '_boxidStr',newboxIdStr);
          }

}